package com.funny.learning.english.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.funny.learning.english.BaseActivity;
import com.funny.learning.english.R;
import com.funny.learning.english.api.BaseAjaxCallBack.IAjaxCallbackMMK;
import com.funny.learning.english.api.Define;
import com.funny.learning.english.api.TiengAnhApi;
import com.funny.learning.english.util.ValidationUtil;

public class GetPwdForgotActivity extends BaseActivity {

	private TextView tvMessage;

	private EditText edtEmail;
	private EditText edtCode;
	private EditText edtNewPass;
	private EditText edtReNewPass;

	private Button btReset;

	private Button btGotoMail;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_getpwdforgot);

		tvMessage = (TextView) findViewById(R.id.tvMessage);

		edtEmail = (EditText) findViewById(R.id.edtEmail);
		edtCode = (EditText) findViewById(R.id.edtCode);
		edtNewPass = (EditText) findViewById(R.id.edtNewPass);
		edtReNewPass = (EditText) findViewById(R.id.edtReNewPass);

		btReset = (Button) findViewById(R.id.btReset); 
		btReset.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getForgotPass();
			}
		});

		btGotoMail = (Button) findViewById(R.id.btGotoMail);
		btGotoMail.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				PackageManager manager = getPackageManager();
				Intent i = manager
						.getLaunchIntentForPackage("com.google.android.gmail");
				i.addCategory(Intent.CATEGORY_LAUNCHER);
				startActivity(i);
			}
		});

		Bundle bundle = getIntent().getExtras();
		String email = bundle.getString(Define.KEY_EMAIL, "");
		edtEmail.setText(email);

		// TiengAnhApi.getForgotPass(email, code, pass, confrimPass, ctx,
		// ajaxCallbackMMK);
	}

	private void getForgotPass() {
		String email = edtEmail.getText().toString().trim();
		String code = edtCode.getText().toString().trim();
		String pass = edtNewPass.getText().toString().trim();
		String confrimPass = edtReNewPass.getText().toString().trim();

		if (!ValidationUtil.isValidEmail(email)) {
			tvMessage.setText(getString(R.string.register_err_email));
			return;
		}

		if (!ValidationUtil.isValidPwd(code)) {
			tvMessage.setText(getString(R.string.forgot_err_code));
			return;
		}

		if (!ValidationUtil.isValidPwd(pass)) {
			tvMessage.setText(getString(R.string.register_err_pass));
			return;
		}

		if (!ValidationUtil.isValidPwd(confrimPass)) {
			tvMessage.setText(getString(R.string.register_err_pass));
			return;
		}

		if (!ValidationUtil.isValidConfirm(pass, confrimPass)) {
			tvMessage.setText(getString(R.string.register_not_is_pass));
			return;
		}

		TiengAnhApi.getForgotPass(email, code, pass, confrimPass, activity,
				ajaxCallbackMMK);
	}

	private IAjaxCallbackMMK ajaxCallbackMMK = new IAjaxCallbackMMK() {

		@Override
		public <T> void handler(T t, int code) {
			// TODO Auto-generated method stub
			if (code == COMPLETE) {
				Toast.makeText(activity, getString(R.string.reset_pass_success),
						Toast.LENGTH_LONG).show();
				Intent intent = new Intent(activity, LoginActivity.class);
				startActivity(intent);
			} else if (code == ERROR) {
				tvMessage.setText(getString(R.string.reset_pass_fail));
			}
		}
	};

}
